<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 获取订单类型
 * @param $type
 * @return mixed|string
 */
function getordtype($type){
    $arr=[
        "1"=>"消费","2"=>"退款","3"=>"充值","4"=>"预授权","5"=>"预授权取消"
    ];
    if(array_key_exists($type,$arr)){
        return $arr[$type];
    }else{
        return "不存在";
    }
}

/**
 * 获取交易状态
 * @param $type
 * @return mixed|string
 */
function getordstatus($type){
    $arr=[
        "1"=>"交易成功","2"=>"待支付","9"=>"待输入密码","4"=>"已取消"
    ];
    if(array_key_exists($type,$arr)){
        return $arr[$type];
    }else{
        return "不存在";
    }
}

/**
 * 格式化订单数据
 */
function parsedata($orderlist){
    $data=[];
    $data["totalnum"]=0;
    $data["tatalincome"]=0;
    $data["alipaynum"]=0;
    $data["alipayincome"]=0;
    $data["wxpaynum"]=0;
    $data["wxpayincome"]=0;
    $data["cardpaynum"]=0;
    $data["cardpayincome"]=0;
    $data["moneypaynum"]=0;
    $data["moneypayincome"]=0;
    if(empty($orderlist)){
        return $data;
    }else{
        foreach ($orderlist as $k=>$v){
            if(($v["ord_type"]=="1")&&($v["ord_status"]=="1")){
                $data["totalnum"]++;
                $data["tatalincome"]+=intval($v["ord_trade_amount"]);
                switch ($v["pmt_name"]){
                    case "支付宝":
                        $data["alipaynum"]++;
                        $data["alipayincome"]+=intval($v["ord_trade_amount"]);
                        break;
                    case "微信支付":
                        $data["wxpaynum"]++;
                        $data["wxpayincome"]+=intval($v["ord_trade_amount"]);
                        break;
                    case "刷卡":
                        $data["cardpaynum"]++;
                        $data["cardpayincome"]+=intval($v["ord_trade_amount"]);
                        break;
                    case "现金收银":
                        $data["moneypaynum"]++;
                        $data["moneypayincome"]+=intval($v["ord_trade_amount"]);
                        break;
                    default:
                        break;
                }
            }else{
                continue;
            }
        }
        return $data;
    }
}


/**
 * 格式化订单数据
 */
function parsebill($orderlist){
    $data=[];
    $data["cashnum"]=0;//应收订单数
    $data["cashincome"]=0;//应收金额(原始金额)
    $data["backnum"]=0;//退款订单数
    $data["backincome"]=0;//退款金额(原始金额)
    $data["xjincome"]=0;//现金收银(原始金额)
    $data["realincome"]=0;//预计到帐
    $data["ord_fee"]=0;//手续费
    $data["ord_discount_amount"]=0;//折扣金额
    if(empty($orderlist)){
        return $data;
    }else{
        foreach ($orderlist as $k=>$v){

            //充值成功
            if(($v["ord_type"]=="1")&&($v["ord_status"]=="1")){
                $data["ord_fee"]+=intval($v["ord_fee"]);//手续费叠加
                $data["ord_discount_amount"]+=intval($v["ord_discount_amount"]);//折扣金额叠加
                $data["cashnum"]++;
                $data["cashincome"]+=intval($v["ord_original_amount"]);
                if($v["pmt_name"]=="现金收银"){
                    $data["xjincome"]+=intval($v["ord_original_amount"]);
                }
            }else if(($v["ord_type"]=="2")&&($v["ord_status"]=="1")){
                $data["ord_fee"]+=intval($v["ord_fee"]);//手续费叠加
                $data["backnum"]++;
                $data["backincome"]+=intval($v["ord_original_amount"]);
                //退款成功
            }else{
                continue;
            }
        }
        $data["realincome"]=$data["cashincome"]-$data["backincome"]-$data["ord_fee"]-$data["xjincome"]-$data["ord_discount_amount"];
        return $data;
    }
}

/**
 * CURL POST拉取数据
 * @param $url
 * @param $data
 * @return mixed
 */
function curl_post($url,$data){
    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $info=curl_exec($curl);
    curl_close($curl);
    return $info;
}

/**
 * POST发送JSON字符串
 * @param $url
 * @param $data_string
 * @return mixed
 */
function curl_post_json($url,$data_string){
    $ch = curl_init($url);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//很重要
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: '.strlen($data_string))
    );
    $info=curl_exec($ch);
    curl_close($ch);
    return $info;
}

/**
 * CURL GET拉取数据
 * @param $url
 * @return mixed
 */
function curl_get($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

if(!function_exists("sendsms")){
    /**
     * 云片发短信
     * @param type $ch
     * @param type $data
     * @return type
     */
    function sendsms($data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间 */
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $data = json_decode(curl_exec($ch), TRUE);
        curl_close($ch);
        return $data;
    }
}

if(!function_exists("isMobiles")){
    /**
     * 验证手机号是否正确
     * @param INT $mobile
     */
    function isMobiles($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }
}

if(!function_exists("printorder")){
    /**
     * @param string $content 打印内容
     * @param type $mcode 机器编码
     * @param type $msign 机器签名
     * 打印内容 2016年9月3日13:53:12
     */
    function printorder($content,$mcode,$msign) {
        $print = new \Yprint();
        $apiKey = "702fe21bc65544bbe8e8e5d0df6b5c88ad8f0907";
        //$msign = 'u53qh8rhttsm';
        //打印
        return $print->action_print(5566,$mcode, $content, $apiKey, $msign);
    }
}

/**
 * 发送一个SOCKET消息
 * @param type $message JSON格式化的字符串
 * @param type $userid 默认为群发
 * 2016年10月31日 10:43:53
 */
function sendsocket($message,$userid=""){
    if(empty($userid)){
        return false;
    }else{
        $tokenres=\think\Db::name("manager")->where(array("id"=>$userid))->column("token")[0];//获取TOKEN
        if(empty($tokenres)){
            return false;//停止
        }
    }
    return curl_get("http://127.0.0.1:2121?type=publish&to={$tokenres}&content={$message}");
}

/**
 * CURL上传文件
 * 适用于PHP55之后
 * @param $url
 * @param $filename
 * @param $path
 * @param $type
 */
function upload_file_55($url,$data){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//还是很重要
    $return_data = curl_exec($ch);
    curl_close($ch);
    return $return_data;
}

/**
 * 生成12为
 * @return string
 */
function generatecode(){
    return str_pad(mt_rand(0,1000000),6,"0",STR_PAD_LEFT).str_pad(mt_rand(0,1000000),6,"0",STR_PAD_LEFT);
}
